﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FinalSettlementModel
{
    public class Permissions : Helper
    {

        // Hàm lấy dữ liệu permission theo id người dùng
        public IQueryable<TblPhanQuyen> permissionUser(int userId)
        {
            var userPer = nc.TblPhanQuyens.Where(s => s.NguoiDungId == userId);
            return userPer;
        }

        // Hàm tạo mới dữ liệu bảng phân quyền cho user
        public void addNewPermission(   int idUser, 
                                        bool TiepNhanHoSo,
                                        bool GiaoHoSo,
                                        bool DuLieuHeThong,
                                        bool NguoiDung,
                                        bool SaoLuu,
                                        bool PhucHoi,
                                        bool GiaoDien,
                                        bool TrinhPheDuyet,
                                        bool PheDuyetQT,
                                        bool BCTongHop,
                                        bool BCHoSoMoi,
                                        bool BCTinhHinhQT,
                                        bool BCTinhHinhHSChuaQT,
                                        bool BCChiTietHSChuaQT,
                                        bool BCChiTietHSDaQT)
        {
            TblPhanQuyen permission = new TblPhanQuyen();
            permission.NguoiDungId = idUser;
            permission.TiepNhanHoSo = TiepNhanHoSo;
            permission.GiaoHoSo = GiaoHoSo;
            permission.DuLieuHeThong = DuLieuHeThong;
            permission.NguoiDung = NguoiDung;
            permission.SaoLuu = SaoLuu;
            permission.PhucHoi = PhucHoi;
            permission.GiaoDien = GiaoDien;
            permission.TrinhPheDuyet = TrinhPheDuyet;
            permission.PheDuyetQT = PheDuyetQT;
            permission.BCTongHop = BCTongHop;
            permission.BCHoSoMoi = BCHoSoMoi;
            permission.BCTinhHinhHSChuaQT = BCTinhHinhHSChuaQT;
            permission.BCTinhHinhQT = BCTinhHinhQT;
            permission.BCChiTietHSChuaQT = BCChiTietHSChuaQT;
            permission.BCChiTietHSDaQT = BCChiTietHSDaQT;
            nc.TblPhanQuyens.InsertOnSubmit(permission);
            nc.SubmitChanges();
        }

        // Hàm chỉnh sữa dữ liệu phân quyền cho user        
        public void updatePermission(int idUser,
                                            bool TiepNhanHoSo,
                                            bool GiaoHoSo,
                                            bool DuLieuHeThong,
                                            bool NguoiDung,
                                            bool SaoLuu,
                                            bool PhucHoi,
                                            bool GiaoDien,
                                            bool TrinhPheDuyet,
                                            bool PheDuyetQT,
                                            bool BCTongHop,
                                            bool BCHoSoMoi,
                                            bool BCTinhHinhQT,
                                            bool BCTinhHinhHSChuaQT,
                                            bool BCChiTietHSChuaQT,
                                            bool BCChiTietHSDaQT)
        {
            var permission = nc.TblPhanQuyens.Single(s => s.NguoiDungId == idUser);            
            permission.TiepNhanHoSo = TiepNhanHoSo;
            permission.GiaoHoSo = GiaoHoSo;
            permission.DuLieuHeThong = DuLieuHeThong;
            permission.NguoiDung = NguoiDung;
            permission.SaoLuu = SaoLuu;
            permission.PhucHoi = PhucHoi;
            permission.GiaoDien = GiaoDien;
            permission.TrinhPheDuyet = TrinhPheDuyet;
            permission.PheDuyetQT = PheDuyetQT;
            permission.BCTongHop = BCTongHop;
            permission.BCHoSoMoi = BCHoSoMoi;
            permission.BCTinhHinhHSChuaQT = BCTinhHinhHSChuaQT;
            permission.BCTinhHinhQT = BCTinhHinhQT;
            permission.BCChiTietHSChuaQT = BCChiTietHSChuaQT;
            permission.BCChiTietHSDaQT = BCChiTietHSDaQT;            
            nc.SubmitChanges();
        }

        // Hàm xoá dữ liệu phân quyền cho user
        public void deletePermission(int userId)
        {            
            var deletePermission = nc.TblPhanQuyens.Single(p => p.NguoiDungId == userId);
            nc.TblPhanQuyens.DeleteOnSubmit(deletePermission);
            nc.SubmitChanges();                
        }
    }
}
